package com.ydd.widegt;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ydd.R;

/**
 * Created by tao on 17-3-23.
 */

public class HeaderView extends RelativeLayout {

    private View mContainerView;
    private TextView mTitleView;
    private ImageView mBackView;
    private TextView mBackTextView;
    private TextView mRightTextView;
    private ImageView mRightImageView;
    private View mShadowView;

    public HeaderView(Context context) {
        super(context);
        init(null, 0);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(getContext()).inflate(R.layout.activity_header_view_layout, this, true);
        mContainerView = findViewById(R.id.header_container);
        mBackView = (ImageView) findViewById(R.id.back_view);
        mBackTextView = (TextView) findViewById(R.id.back_text);
        mTitleView = (TextView) findViewById(R.id.title_view);
        mRightImageView = (ImageView) findViewById(R.id.right_view);
        mRightTextView = (TextView) findViewById(R.id.right_text);
        mShadowView = findViewById(R.id.shadow);

        mBackView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).onBackPressed();
            }
        });
        showLeft(false);
    }

    public void showLeft(boolean show) {
        mBackView.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showLeftView(boolean show, OnClickListener listener) {
        mBackView.setOnClickListener(listener);
        mBackView.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showLeftTextView(boolean show, String text, OnClickListener listener) {
        mBackView.setVisibility(show ? View.GONE : View.VISIBLE);
        mBackTextView.setOnClickListener(listener);
        mBackTextView.setText(text);
        mBackTextView.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showTitleView(String text) {
        mTitleView.setText(text);
    }

    public void showRightImageView(boolean show, int drawId, OnClickListener listener) {
        mRightImageView.setOnClickListener(listener);
        mRightImageView.setImageResource(drawId);
        mRightImageView.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showRightTextView(boolean show, String text) {
        mRightTextView.setVisibility(show ? View.VISIBLE : View.GONE);
        mRightTextView.setText(text);
    }

    public void hideShadow() {
        mShadowView.setVisibility(View.GONE);
    }

    public void setBgColor(int color) {
        mContainerView.setBackgroundColor(color);
    }

    public void setBgImage(int bgImage) {
        mContainerView.setBackgroundResource(bgImage);
    }
}
